<template><div><h2 id="控制台应用" tabindex="-1"><a class="header-anchor" href="#控制台应用"><span>控制台应用</span></a></h2>
<p>Besides the rich features for building web applications, Yii also has full-featured support for console applications which are mainly used to create background and maintenance tasks that need to be performed for a website.</p>
<p>The structure of console applications is very similar to a Yii web application. It consists of one or more [[yii\console\Controller]] classes, which are often referred to as <em>commands</em> in the console environment. Each controller can also have one or more actions, just like web controllers.</p>
<p>Both project templates already have a console application with them. You can run it by calling the <code v-pre>yii</code> script, which is located in the base directory of the repository. This will give you a list of available commands when you run it without any further parameters:</p>
<p><img src="https://cdn.learnku.com/uploads/images/202202/28/93088/biPAue8sbK.png!large" alt="Running ./yii command for help output"></p>
<p>As you can see in the screenshot, Yii has already defined a set of commands that are available by default:</p>
<ul>
<li>[[yii\console\controllers\AssetController|AssetController]] - Allows you to combine and compress your JavaScript and CSS files. You can learn more about this command in the <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide/structure-assets.md#using-asset-bundles" target="_blank" rel="noopener noreferrer">Assets Section</a>.</li>
<li>[[yii\console\controllers\CacheController|CacheController]] - Allows you to flush application caches.</li>
<li>[[yii\console\controllers\FixtureController|FixtureController]] - Manages fixture data loading and unloading for testing purposes. This command is described in more detail in the <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide/test-fixtures.md#managing-fixtures" target="_blank" rel="noopener noreferrer">Testing Section about Fixtures</a>.</li>
<li>[[yii\console\controllers\HelpController|HelpController]] - Provides help information about console commands, this is the default command and prints what you have seen in the above output.</li>
<li>[[yii\console\controllers\MessageController|MessageController]] - Extracts messages to be translated from source files. To learn more about this command, please refer to the <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide/tutorial-i18n.md#message-command" target="_blank" rel="noopener noreferrer">I18N Section</a>.</li>
<li>[[yii\console\controllers\MigrateController|MigrateController]] - Manages application migrations. Database migrations are described in more detail in the <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide/db-migrations.md" target="_blank" rel="noopener noreferrer">Database Migration Section</a>.</li>
<li>[[yii\console\controllers\ServeController|ServeController]] - Allows you run PHP built-in web server.</li>
</ul>
<h2 id="usage" tabindex="-1"><a class="header-anchor" href="#usage"><span><a href="https://github.com/yiisoft/yii2/blob/master/docs/guide/tutorial-console.md#usage-" target="_blank" rel="noopener noreferrer"></a>Usage</span></a></h2>
<p>You execute a console controller action using the following syntax:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">yii <span class="token operator">&lt;</span>route<span class="token operator">></span> <span class="token punctuation">[</span><span class="token operator">--</span>option1<span class="token operator">=</span>value1 <span class="token operator">...</span> argument1 argument2 <span class="token operator">...</span> <span class="token operator">--</span>option2<span class="token operator">=</span>value2<span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>Options could be specified in any position.</p>
<p>In the above, <code v-pre>&lt;route&gt;</code> refers to the route to the controller action. The options will populate the class properties and arguments are the parameters of the action method.</p>
<p>For example, the [[yii\console\controllers\MigrateController::actionUp()|MigrateController::actionUp()]] with [[yii\console\controllers\MigrateController::$migrationTable|MigrateController::$migrationTable]] set to <code v-pre>migrations</code> and a limit of 5 migrations can be called like so:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">yii migrate<span class="token operator">/</span>up <span class="token number">5</span> <span class="token operator">--</span>migrationTable<span class="token operator">=</span>migrations</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p>Note: When using <code v-pre>*</code> in console, don’t forget to quote it as <code v-pre>&quot;*&quot;</code> in order to avoid executing it as a shell glob that will be replaced by all file names of the current directory.</p>
</blockquote>
<h2 id="the-entry-script" tabindex="-1"><a class="header-anchor" href="#the-entry-script"><span><a href="https://github.com/yiisoft/yii2/blob/master/docs/guide/tutorial-console.md#the-entry-script-" target="_blank" rel="noopener noreferrer"></a>The entry script</span></a></h2>
<p>The console application entry script is equivalent to the <code v-pre>index.php</code> bootstrap file used for the web application. The console entry script is typically called <code v-pre>yii</code>, and located in your application’s root directory. It contains code like the following:</p>
<div class="language-text-html-php line-numbers-mode" data-highlighter="prismjs" data-ext="text-html-php" data-title="text-html-php"><pre v-pre class="language-text-html-php"><code><span class="line">#!/usr/bin/env php</span>
<span class="line">&lt;?php</span>
<span class="line">/**</span>
<span class="line"> * Yii console bootstrap file.</span>
<span class="line"> */</span>
<span class="line"></span>
<span class="line">defined('YII_DEBUG') or define('YII_DEBUG', true);</span>
<span class="line">defined('YII_ENV') or define('YII_ENV', 'dev');</span>
<span class="line"></span>
<span class="line">require __DIR__ . '/vendor/autoload.php';</span>
<span class="line">require __DIR__ . '/vendor/yiisoft/yii2/Yii.php';</span>
<span class="line"></span>
<span class="line">$config = require __DIR__ . '/config/console.php';</span>
<span class="line"></span>
<span class="line">$application = new yii\console\Application($config);</span>
<span class="line">$exitCode = $application-&gt;run();</span>
<span class="line">exit($exitCode);</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>This script will be created as part of your application; you’re free to edit it to suit your needs. The <code v-pre>YII_DEBUG</code> constant can be set to <code v-pre>false</code> if you do not want to see a stack trace on error, and/or if you want to improve the overall performance. In both basic and advanced application templates, the console application entry script has debugging enabled by default to provide a more developer-friendly environment.</p>
<h2 id="configuration" tabindex="-1"><a class="header-anchor" href="#configuration"><span><a href="https://github.com/yiisoft/yii2/blob/master/docs/guide/tutorial-console.md#configuration-" target="_blank" rel="noopener noreferrer"></a>Configuration</span></a></h2>
<p>As can be seen in the code above, the console application uses its own configuration file, named <code v-pre>console.php</code>. In this file you should configure various <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide/structure-application-components.md" target="_blank" rel="noopener noreferrer">application components</a> and properties for the console application in particular.</p>
<p>If your web application and console application share a lot of configuration parameters and values, you may consider moving the common parts into a separate file, and including this file in both of the application configurations (web and console). You can see an example of this in the advanced project template.</p>
<blockquote>
<p>Tip: Sometimes, you may want to run a console command using an application configuration that is different from the one specified in the entry script. For example, you may want to use the <code v-pre>yii migrate</code> command to upgrade your test databases, which are configured in each individual test suite. To change the configuration dynamically, simply specify a custom application configuration file via the <code v-pre>appconfig</code> option when executing the command:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">yii <span class="token operator">&lt;</span>route<span class="token operator">></span> <span class="token operator">--</span>appconfig<span class="token operator">=</span>path<span class="token operator">/</span>to<span class="token operator">/</span>config<span class="token operator">.</span>php <span class="token operator">...</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div></blockquote>
<h2 id="console-command-completion" tabindex="-1"><a class="header-anchor" href="#console-command-completion"><span><a href="https://github.com/yiisoft/yii2/blob/master/docs/guide/tutorial-console.md#console-command-completion-" target="_blank" rel="noopener noreferrer"></a>Console command completion</span></a></h2>
<p>Auto-completion of command arguments is a useful thing when working with the shell. Since version 2.0.11, the <code v-pre>./yii</code> command provides auto completion for the Bash and ZSH out of the box.</p>
<h3 id="bash-completion" tabindex="-1"><a class="header-anchor" href="#bash-completion"><span><a href="https://github.com/yiisoft/yii2/blob/master/docs/guide/tutorial-console.md#bash-completion" target="_blank" rel="noopener noreferrer"></a>Bash completion</span></a></h3>
<p>Make sure bash completion is installed. For most of installations it is available by default.</p>
<p>Place the completion script in <code v-pre>/etc/bash_completion.d/</code>:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"> curl <span class="token operator">-</span><span class="token constant">L</span> https<span class="token punctuation">:</span><span class="token comment">//raw.githubusercontent.com/yiisoft/yii2/master/contrib/completion/bash/yii -o /etc/bash_completion.d/yii</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>For temporary usage you can put the file into the current directory and include it in the current session via <code v-pre>source yii</code>. If globally installed you may need to restart the terminal or <code v-pre>source ~/.bashrc</code> to activate it.</p>
<p>Check the <a href="https://www.gnu.org/software/bash/manual/html_node/Programmable-Completion.html" target="_blank" rel="noopener noreferrer">Bash Manual</a> for other ways of including completion script to your environment.</p>
<h3 id="zsh-completion" tabindex="-1"><a class="header-anchor" href="#zsh-completion"><span><a href="https://github.com/yiisoft/yii2/blob/master/docs/guide/tutorial-console.md#zsh-completion" target="_blank" rel="noopener noreferrer"></a>ZSH completion</span></a></h3>
<p>Put the completion script in directory for completions, using e.g. <code v-pre>~/.zsh/completion/</code></p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">mkdir <span class="token operator">-</span>p <span class="token operator">~</span><span class="token operator">/</span><span class="token operator">.</span>zsh<span class="token operator">/</span>completion</span>
<span class="line">curl <span class="token operator">-</span><span class="token constant">L</span> https<span class="token punctuation">:</span><span class="token comment">//raw.githubusercontent.com/yiisoft/yii2/master/contrib/completion/zsh/_yii -o ~/.zsh/completion/_yii</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><p>Include the directory in the <code v-pre>$fpath</code>, e.g. by adding it to <code v-pre>~/.zshrc</code></p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">fpath<span class="token operator">=</span><span class="token punctuation">(</span><span class="token operator">~</span><span class="token operator">/</span><span class="token operator">.</span>zsh<span class="token operator">/</span><span class="token class-name type-declaration">completion</span> <span class="token variable">$fpath</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>Make sure <code v-pre>compinit</code> is loaded or do it by adding in <code v-pre>~/.zshrc</code></p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">autoload <span class="token operator">-</span>Uz compinit <span class="token operator">&amp;&amp;</span> compinit <span class="token operator">-</span>i</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>Then reload your shell</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name type-declaration">exec</span> <span class="token variable">$SHELL</span> <span class="token operator">-</span>l</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="creating-your-own-console-commands" tabindex="-1"><a class="header-anchor" href="#creating-your-own-console-commands"><span><a href="https://github.com/yiisoft/yii2/blob/master/docs/guide/tutorial-console.md#creating-your-own-console-commands-" target="_blank" rel="noopener noreferrer"></a>Creating your own console commands</span></a></h2>
<h3 id="console-controller-and-action" tabindex="-1"><a class="header-anchor" href="#console-controller-and-action"><span><a href="https://github.com/yiisoft/yii2/blob/master/docs/guide/tutorial-console.md#console-controller-and-action" target="_blank" rel="noopener noreferrer"></a>Console Controller and Action</span></a></h3>
<p>A console command is defined as a controller class extending from [[yii\console\Controller]]. In the controller class, you define one or more actions that correspond to sub-commands of the controller. Within each action, you write code that implements the appropriate tasks for that particular sub-command.</p>
<p>When running a command, you need to specify the route to the controller action. For example, the route <code v-pre>migrate/create</code> invokes the sub-command that corresponds to the [[yii\console\controllers\MigrateController::actionCreate()|MigrateController::actionCreate()]] action method. If a route offered during execution does not contain an action ID, the default action will be executed (as with a web controller).</p>
<h3 id="options" tabindex="-1"><a class="header-anchor" href="#options"><span><a href="https://github.com/yiisoft/yii2/blob/master/docs/guide/tutorial-console.md#options" target="_blank" rel="noopener noreferrer"></a>Options</span></a></h3>
<p>By overriding the [[yii\console\Controller::options()]] method, you can specify options that are available to a console command (controller/actionID). The method should return a list of the controller class’s public properties. When running a command, you may specify the value of an option using the syntax <code v-pre>--optionName=optionValue</code>. This will assign <code v-pre>optionValue</code> to the <code v-pre>optionName</code> property of the controller class.</p>
<p>If the default value of an option is of an array type and you set this option while running the command, the option value will be converted into an array by splitting the input string on any commas.</p>
<h3 id="options-aliases" tabindex="-1"><a class="header-anchor" href="#options-aliases"><span><a href="https://github.com/yiisoft/yii2/blob/master/docs/guide/tutorial-console.md#options-aliases" target="_blank" rel="noopener noreferrer"></a>Options Aliases</span></a></h3>
<p>Since version 2.0.8 console command provides [[yii\console\Controller::optionAliases()]] method to add aliases for options.</p>
<p>To define an alias, override [[yii\console\Controller::optionAliases()]] in your controller, for example:</p>
<div class="language-text-html-php line-numbers-mode" data-highlighter="prismjs" data-ext="text-html-php" data-title="text-html-php"><pre v-pre class="language-text-html-php"><code><span class="line">namespace app\commands;</span>
<span class="line"></span>
<span class="line">use yii\console\Controller;</span>
<span class="line"></span>
<span class="line">class HelloController extends Controller</span>
<span class="line">{</span>
<span class="line">    public $message;</span>
<span class="line"></span>
<span class="line">    public function options($actionID)</span>
<span class="line">    {</span>
<span class="line">        return ['message'];</span>
<span class="line">    }</span>
<span class="line"></span>
<span class="line">    public function optionAliases()</span>
<span class="line">    {</span>
<span class="line">        return ['m' =&gt; 'message'];</span>
<span class="line">    }</span>
<span class="line"></span>
<span class="line">    public function actionIndex()</span>
<span class="line">    {</span>
<span class="line">        echo $this-&gt;message . &quot;\n&quot;;</span>
<span class="line">    }</span>
<span class="line">}</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Now, you can use the following syntax to run the command:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">yii hello <span class="token operator">-</span>m<span class="token operator">=</span>hello</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="arguments" tabindex="-1"><a class="header-anchor" href="#arguments"><span><a href="https://github.com/yiisoft/yii2/blob/master/docs/guide/tutorial-console.md#arguments" target="_blank" rel="noopener noreferrer"></a>Arguments</span></a></h3>
<p>Besides options, a command can also receive arguments. The arguments will be passed as the parameters to the action method corresponding to the requested sub-command. The first argument corresponds to the first parameter, the second corresponds to the second, and so on. If not enough arguments are provided when the command is called, the corresponding parameters will take the declared default values, if defined. If no default value is set, and no value is provided at runtime, the command will exit with an error.</p>
<p>You may use the <code v-pre>array</code> type hint to indicate that an argument should be treated as an array. The array will be generated by splitting the input string on commas.</p>
<p>The following example shows how to declare arguments:</p>
<div class="language-text-html-php line-numbers-mode" data-highlighter="prismjs" data-ext="text-html-php" data-title="text-html-php"><pre v-pre class="language-text-html-php"><code><span class="line">class ExampleController extends \yii\console\Controller</span>
<span class="line">{</span>
<span class="line">    // The command &quot;yii example/create test&quot; will call &quot;actionCreate('test')&quot;</span>
<span class="line">    public function actionCreate($name) { ... }</span>
<span class="line"></span>
<span class="line">    // The command &quot;yii example/index city&quot; will call &quot;actionIndex('city', 'name')&quot;</span>
<span class="line">    // The command &quot;yii example/index city id&quot; will call &quot;actionIndex('city', 'id')&quot;</span>
<span class="line">    public function actionIndex($category, $order = 'name') { ... }</span>
<span class="line"></span>
<span class="line">    // The command &quot;yii example/add test&quot; will call &quot;actionAdd(['test'])&quot;</span>
<span class="line">    // The command &quot;yii example/add test1,test2&quot; will call &quot;actionAdd(['test1', 'test2'])&quot;</span>
<span class="line">    public function actionAdd(array $name) { ... }</span>
<span class="line">}</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="exit-code" tabindex="-1"><a class="header-anchor" href="#exit-code"><span><a href="https://github.com/yiisoft/yii2/blob/master/docs/guide/tutorial-console.md#exit-code" target="_blank" rel="noopener noreferrer"></a>Exit Code</span></a></h3>
<p>Using exit codes is a best practice for console application development. Conventionally, a command returns <code v-pre>0</code> to indicate that everything is OK. If the command returns a number greater than zero, that’s considered to be indicative of an error. The number returned will be the error code, potentially usable to find out details about the error. For example <code v-pre>1</code> could stand generally for an unknown error and all codes above would be reserved for specific cases: input errors, missing files, and so forth.</p>
<p>To have your console command return an exit code, simply return an integer in the controller action method:</p>
<div class="language-text-html-php line-numbers-mode" data-highlighter="prismjs" data-ext="text-html-php" data-title="text-html-php"><pre v-pre class="language-text-html-php"><code><span class="line">public function actionIndex()</span>
<span class="line">{</span>
<span class="line">    if (/* some problem */) {</span>
<span class="line">        echo &quot;A problem occurred!\n&quot;;</span>
<span class="line">        return 1;</span>
<span class="line">    }</span>
<span class="line">    // do something</span>
<span class="line">    return 0;</span>
<span class="line">}</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>There are some predefined constants you can use. These are defined in the [[yii\console\ExitCode]] class:</p>
<div class="language-text-html-php line-numbers-mode" data-highlighter="prismjs" data-ext="text-html-php" data-title="text-html-php"><pre v-pre class="language-text-html-php"><code><span class="line">public function actionIndex()</span>
<span class="line">{</span>
<span class="line">    if (/* some problem */) {</span>
<span class="line">        echo &quot;A problem occurred!\n&quot;;</span>
<span class="line">        return ExitCode::UNSPECIFIED_ERROR;</span>
<span class="line">    }</span>
<span class="line">    // do something</span>
<span class="line">    return ExitCode::OK;</span>
<span class="line">}</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>It’s a good practice to define meaningful constants for your controller in case you have more specific error code types.</p>
<h3 id="formatting-and-colors" tabindex="-1"><a class="header-anchor" href="#formatting-and-colors"><span><a href="https://github.com/yiisoft/yii2/blob/master/docs/guide/tutorial-console.md#formatting-and-colors" target="_blank" rel="noopener noreferrer"></a>Formatting and colors</span></a></h3>
<p>Yii console supports formatted output that is automatically degraded to non-formatted one if it’s not supported by terminal running the command.</p>
<p>Outputting formatted strings is simple. Here’s how to output some bold text:</p>
<div class="language-text-html-php line-numbers-mode" data-highlighter="prismjs" data-ext="text-html-php" data-title="text-html-php"><pre v-pre class="language-text-html-php"><code><span class="line">$this-&gt;stdout(&quot;Hello?\n&quot;, Console::BOLD);</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>If you need to build string dynamically combining multiple styles it’s better to use [[yii\helpers\Console::ansiFormat()|ansiFormat()]]:</p>
<div class="language-text-html-php line-numbers-mode" data-highlighter="prismjs" data-ext="text-html-php" data-title="text-html-php"><pre v-pre class="language-text-html-php"><code><span class="line">$name = $this-&gt;ansiFormat('Alex', Console::FG_YELLOW);</span>
<span class="line">echo &quot;Hello, my name is $name.&quot;;</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="tables" tabindex="-1"><a class="header-anchor" href="#tables"><span><a href="https://github.com/yiisoft/yii2/blob/master/docs/guide/tutorial-console.md#tables" target="_blank" rel="noopener noreferrer"></a>Tables</span></a></h3>
<p>Since version 2.0.13 there is a widget that allows you to format table data in console. It could be used as the following:</p>
<div class="language-text-html-php line-numbers-mode" data-highlighter="prismjs" data-ext="text-html-php" data-title="text-html-php"><pre v-pre class="language-text-html-php"><code><span class="line">echo Table::widget([</span>
<span class="line">    'headers' =&gt; ['Project', 'Status', 'Participant'],</span>
<span class="line">    'rows' =&gt; [</span>
<span class="line">        ['Yii', 'OK', '@samdark'],</span>
<span class="line">        ['Yii', 'OK', '@cebe'],</span>
<span class="line">    ],</span>
<span class="line">]);</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>For details please refer to [[yii\console\widgets\Table|API documentation]].</p>
<blockquote>
<p>💖喜欢本文档的，欢迎点赞、收藏、留言或转发，谢谢支持！<br>
作者邮箱：zhuzixian520@126.com，github地址：<a href="https://github.com/zhuzixian520" target="_blank" rel="noopener noreferrer">github.com/zhuzixian520</a></p>
</blockquote>
</div></template>


